<template>
  <div>

    <el-dialog title="上级组织" :visible.sync="dialogPIdFormVisible">
      <el-tree
        :data="deptList"
        check-strictly
        show-checkbox
        default-expand-all
        node-key="id"
        ref="tree"
        highlight-current
        getCurrentNode
        @node-click="_nodeClick" @check-change="_checkChange"
        :props="defaultProps">
      </el-tree>

      <div class="buttons">
        <el-button @click="getCheckedNodes">确定</el-button>
      </div>

    </el-dialog>

  </div>

</template>

<script>

    export default {

      mounted(){
        this._listParent();
      },

        name: "SysMenuEdit",
      data(){
          return{

            deptList: [],
            defaultProps: {
              children: 'children',
              label: 'label'
            },

            dialogPIdFormVisible: false,
            editCheckId: '',
            formLabelWidth: '120px'
          }
      },
      methods:{

          _open(){
            this.dialogPIdFormVisible=true;
            this._getDeptList();
          },
        getCheckedNodes() {
          let data= this.$refs.tree.getCheckedNodes();
          console.log('=========',data);
          this.$emit('on-result-change',data[0])
          this.dialogPIdFormVisible=false;
        },

        _getDeptList(){
          this.$http.post('/api/base/sys_dept/get_dept_dto_list',{}).then(res=>{
            let data= res.data.data;
            this.deptList=data;
            console.log('----------------data',data)
          }).catch(err=>{
            console.log(err)
          });
        },


        _pNameChange(data){
            this.form.pid=data;
        },


        _pnameClick(){

        },
// 节点的点击事件
        _nodeClick(item, node, self) {
          console.warn('第一项', item);
          console.warn('第二项', node);
          console.warn('第三项', self);
          //  设置当前的选中的数据
          this.editCheckId = item.id;
          this.$refs.tree.setCheckedKeys([item.id])
        },
        // 数据变化时的回调
        _checkChange(item, isSelfChecked, isChildChecked) {
          // console.warn('第一项', item);
          // console.warn('第二项', isSelfChecked);
          // console.warn('第三项', self);
          if (isSelfChecked === true) {
            this.editCheckId = item.id;
            this.$refs.tree.setCheckedKeys([item.id])
          } else {
            if (this.editCheckId === item.id) {
              this.$refs.tree.setCheckedKeys([item.id])
            }
          }
        },

        _listParent(){
          this.$http.get('/api/base/sys_resource/list_parent').then(res=>{
                this.pNameList=res.data.data;
          }).catch(err=>{

          });
        },


          _ok(){
            let userData= this.form;
            if (undefined === userData.id){
              this.$http.post('/sys_resource/add',userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '添加失败'
                });
              })

            } else {
              this.$http.put('/sys_resource/'+userData.id,userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '编辑失败'
                });
              })
            }




          }

      },
    }
</script>

<style scoped>

</style>
